package samsung.sds.departmentManagement.service.impl;


import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import samsung.sds.departmentManagement.dao.DepartmentMapper;
import samsung.sds.departmentManagement.model.DepartmentEntity;
import samsung.sds.departmentManagement.service.DepartmentManagementService;
import samsung.sds.system.common.baseQuery.BaseMapper;
import samsung.sds.system.common.baseQuery.AbstractBaseService;
import samsung.sds.system.common.baseQuery.model.PageData;
import samsung.sds.system.common.model.QueryParam;
import samsung.sds.system.utils.Constants;
import samsung.sds.userManagement.model.UserEntity;
import samsung.sds.userManagement.service.UserManagementService;

/**
 * @author wch.wu
 * @createDate 2016-01-15
 *
 * 组织机构管理Service实现类
 */
@Service
public class DepartmentManagementServiceImpl extends AbstractBaseService<DepartmentEntity> implements DepartmentManagementService{
	
	@Autowired
	private DepartmentMapper departmentMapper;
	
	@Autowired
	private UserManagementService userManagementServiceImpl;

	@Override
	protected BaseMapper<DepartmentEntity> getMapper() {
		return departmentMapper;
	}

	/**
	 * 根据组织机构节点查询所有后代节点和自身节点信息
	 */
	public List<DepartmentEntity> queryDepartmentList(String deptId){
		return departmentMapper.selectSYS_DEPARTMENT02( deptId );
	}
	
	/**
	 * 插入新建节点，同时修改父节点本身状态
	 */
	@Override
	public int insertEntity(DepartmentEntity departmentEntity) {
		
		// 1、插入子节点数据
		int count = super.insertEntity( departmentEntity );
		
		DepartmentEntity fatherEntity = new DepartmentEntity();
		fatherEntity.setDeptId( departmentEntity.getParentId() );
		fatherEntity = departmentMapper.selectSYS_DEPARTMENT01( fatherEntity );
		
		// 2、如果父节点是"叶子节点"状态（代表第一次在这个节点下创建子节点，则需要修改为"非叶子节点"）
		if( Constants.DEPARTMENT_ISLEAF_YES.equals( fatherEntity.getIsLeaf()) ){
			fatherEntity.setIsLeaf( Constants.DEPARTMENT_ISLEAF_NOT );
			updateEntity(fatherEntity);
			
			// 3、把以前挂在父节点下的用户迁移到新的叶子节点下
			String newDeptId = departmentEntity.getDeptId();
			String oldDeptId = departmentEntity.getParentId();
			userManagementServiceImpl.updateUserDepartment(newDeptId, oldDeptId);
		}
		
		return count;
	}
	
	/**
	 * 用户删除节点的时候业务逻辑（前提所有后代节点下都没有用户数据）
	 * @param departmentEntities
	 */
	public int deleteEntitys( List<DepartmentEntity> departmentEntities, String deptId ){
		
		// 删除这个组织机构和所有后代组织机构
		int count = departmentMapper.deleteSYS_DEPARTMENT(departmentEntities);

		// 判断父节点还有没有其他子节点，如果没有，修改父节点为叶子节点
		DepartmentEntity fatherDepartment = new DepartmentEntity();
		for (DepartmentEntity tempDepartmentEntity : departmentEntities) {
			
			// 如果是该节点本身，则获取父节点ID
			if( tempDepartmentEntity.getDeptId().compareTo( deptId ) == 0 ){
				fatherDepartment.setDeptId( tempDepartmentEntity.getParentId() );
			}
		}

		List<DepartmentEntity> fatherDepartmentEntities = queryDepartmentList( fatherDepartment.getDeptId() );
		if( fatherDepartmentEntities.size() == 1 ){
			fatherDepartment.setIsLeaf( Constants.DEPARTMENT_ISLEAF_YES );
			
			// 更新为叶子节点
			updateEntity( fatherDepartment );
		}
		return count;
	}
	
	/**
	 * 查询用户分页列表（方法重写）
	 * @param param
	 * @return
	 */
	public PageData<UserEntity> queryDepartmentUsersListPage(QueryParam param) {
		List<UserEntity> list = departmentMapper.queryDepartmentUsersListPage(param);
		PageData<UserEntity> pd = new PageData<UserEntity>();
		pd.setQueryResult(list);
		pd.setQueryParam(param);
		return pd;
	}
	
	
	/**
	 * 查询子节点
	 * @param param
	 * @return
	 */
	public List<DepartmentEntity> queryListByParentId(QueryParam param){
		return departmentMapper.selectSYS_DEPARTMENT(param);
	}


}

